#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2020/8/4 下午2:47
# @Author  : fugang_le
# @Software: PyCharm

########### ac自动机 ###########
import ahocorasick
A = ahocorasick.Automaton()

for idx, key in enumerate(["苹果", "香蕉", "梨", "橙子", "柚子", "火龙果", "柿子", "猕猴挑"]):
    A.add_word(key, (idx, key))

print( '橙子' in A)
# print('HER' in A)

# print(A.get('he'))
# print(A.get('she'))
# print(A.get('cat', 'not exists'))
# print(A.get('dog'))

A.make_automaton()
haystack = '我最喜欢吃的水果有：苹果、梨和香蕉'
for end_index, (insert_order, original_value) in A.iter(haystack):
     start_index = end_index - len(original_value) + 1
     print((start_index, end_index, (insert_order, original_value)))
     assert haystack[start_index:start_index + len(original_value)] == original_value
for item in A.iter(haystack):
    print(item)
